package flc.ast.fragment;

import static android.app.Activity.RESULT_OK;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.FileUtils;
import com.stark.doc.selector.DefDocSelActivity;

import java.util.ArrayList;

import flc.ast.R;
import flc.ast.activity.MdConvertActivity;
import flc.ast.activity.SelVideoActivity;
import flc.ast.databinding.FragmentMdConvertBinding;
import flc.ast.utils.Content;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.constant.Extra;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.media.MediaInfo;
import stark.common.basic.utils.StkPermissionHelper;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link MdConvertFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MdConvertFragment extends BaseNoModelFragment<FragmentMdConvertBinding> {

    private static final int REQ_SEL_MD = 1010;
    private static final int REQ_CONVERT_MD = 1020;
    private String path;

    @Override
    protected int onCreate() {
        return R.layout.fragment_md_convert;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);

        mDataBinding.ivAdd.setOnClickListener(this);
        mDataBinding.llFile.setOnClickListener(this);
        mDataBinding.ivConvertWord.setOnClickListener(this);
        mDataBinding.ivConvertPdf.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivAdd://添加文件
            case R.id.llFile:
                getPermission();
                break;
            case R.id.ivConvertWord://markdown转word
                MdConvertActivity.type = 0;
                MdConvertActivity.sPath = path;
                Intent intent = new Intent(getContext(), MdConvertActivity.class);
                startActivityForResult(intent, REQ_CONVERT_MD);
                break;
            case R.id.ivConvertPdf://markdown转pdf
                MdConvertActivity.type = 1;
                MdConvertActivity.sPath = path;
                Intent intent1 = new Intent(getContext(), MdConvertActivity.class);
                startActivityForResult(intent1, REQ_CONVERT_MD);
                break;
        }
    }

    @Override
    protected void initData() {

    }

    private void getPermission() {
        StkPermissionHelper.reqManageExternalStoragePermission().reqPermissionDesc(getString(R.string.get_markdown_permission)).callback(new StkPermissionHelper.ACallback() {
            @Override
            public void onGranted() {
                ArrayList<String> typeList = new ArrayList<>();
                typeList.add("text/markdown");
                DefDocSelActivity.selFile(MdConvertFragment.this, typeList, REQ_SEL_MD);
            }
        }).request();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) return;
        if (requestCode == REQ_SEL_MD) {
            if (data == null) return;
            MediaInfo mediaInfo = (MediaInfo) data.getSerializableExtra(Extra.DATA);
            path = mediaInfo.getPath();
            mDataBinding.tvFileName.setText(FileUtils.getFileName(path));
            mDataBinding.rlAddFile.setVisibility(View.GONE);
            mDataBinding.llConvert.setVisibility(View.VISIBLE);
        } else if (requestCode == REQ_CONVERT_MD) {
            mDataBinding.rlAddFile.setVisibility(View.VISIBLE);
            mDataBinding.llConvert.setVisibility(View.GONE);
        }
    }

}